package com.fcat.base.common.util;



import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;

public class AESAndC02 {

    /**
     * 加密
     *
     * @param b
     * @return
     */
    public static byte[] encryptAES(byte[] b, String aesKey,String charsetName) {
        if (aesKey == null) {
            System.out.print("Key为空null");
            return null;
        }
        //判断Key是否为16位
        if (aesKey.length() != 16) {
            System.out.print("Key长度不是16位");
            return null;
        }
        try {
            byte[] raw = aesKey.getBytes(charsetName);  //DataDict.CHAR_SET
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] encrypted = cipher.doFinal(b);
            return encrypted;
        } catch (Exception e) {
//            log.error("数据加密时发生异常...", e);
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 加密
     *
     * @param b
     * @return
     */
    public static byte[] encryptAES(byte[] b, String aesKey) {
        return encryptAES(b,aesKey, "utf-8");

    }
    /**
     * 解密
     *
     * @param b
     * @return
     */
    public static byte[] decryptAES(byte[] b, String aesKey) {
        return decryptAES(b,aesKey,"utf-8");
    }
    /**
     * 解密
     *
     * @param b
     * @return
     */
    public static byte[] decryptAES(byte[] b, String aesKey,String charsetName) {
        //判断Key是否正确  
        if (aesKey == null) {
            System.out.print("Key为空null");
            return null;
        }
        //判断Key是否为16位  
        if (aesKey.length() != 16) {
            System.out.print("Key长度不是16位");
            return null;
        }
        try {
            byte[] raw = aesKey.getBytes(charsetName);  //DataDict.CHAR_SET
            SecretKeySpec skp = new SecretKeySpec(raw, "AES");

            Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
            cipher.init(Cipher.DECRYPT_MODE, skp);
            byte[] original = cipher.doFinal(b);
            return original;
        } catch (Exception e) {
            e.printStackTrace();
//            log.error("数据解密时发生异常...", e);
        }
        return null;
    }

    public static void main(String[] args) throws Exception {

        String aesKey = "qwe4567zxc177456";
        aesKey = "vqe1iOREC1SmIEvy";
        String source = "$MGV002,869152027726776,,R,230916,105549,V,2238.21280,N,11401.96985,E,00,00,00,99.9,,,,,460,00,262C,10F7,17,,,,,,,,0,10,100,Timer;!";
        // GPS 设备上传数据示例
        String source1 = "$GPRS,869152027726776;R029;!" + "100000000010001";
        String gg = "A12C3DDE355F9D68D221291A1ABCB603B9CF21A63F3B28AFCDD9ED113257B210C31EF89345EAD62F775AEC076FC323648528717502935078266104C8DAE520EF25480711D7CAA12E250589D94BD459607008BC092548EBBF85B36ED692E6075FC1600E5B55EDDD5E97791503790EF7A094DACC7F811BF6192026C7564549FE83546D71133A41224FD9D17C41296F4194";

        String gg1 = "98384171D1B38DED8E3A9C356CEBF813E971A4336EEB84C7BDB6CA2940EAF546";
        gg1 = "019631416D6C4588B26EE361698738C9DC090E9B4651ED3118238422E6FA689D";

        String pk = "00B1000001000200363037656139333162373938343666393865656200020083A12C3DDE355F9D68D221291A1ABCB6035BAA7E7EFE1742285842E7E96B8DB4F083C1092AF4875B3F39060A27865B24CEF3EE27B2040B2DC6DFBC74F4B3E0FAEEC8372B461098E3D67C286B2231DDAA777008BC092548EBBF85B36ED692E6075F0507BE30E5F6F2B6ECD8E17BE0E58DE723CB04CFBDAF7E784D2EBCB47FF826A6546D71133A41224FD9D17C41296F4194";

        //    System.out.println(Convert.bytesToHexString(encryptAES(source1.getBytes(DataDict.CHAR_SET), aesKey)));
        System.out.println(new String(decryptAES(Convert.hexStringToByte(gg1), aesKey), StandardCharsets.UTF_8));


        System.out.println(Integer.parseInt("001C", 16));

        byte msg[] = Convert.arraycat(Convert.hexStringToByte(pk), aesKey.getBytes(StandardCharsets.UTF_8));

      /*  Packet p = ParseKafka.getPacket(msg);
        System.out.println("----" + p.getPacketContent());
*/

    }
}